/**
 * 
 */
package com.base.myproject.server.ratify;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.base.myproject.server.database.PoolMan;

/**
 * 审批，添加审批object，整理上下游关系，处理是否能审批，
 * @author zgw
 *2009-12-2 上午07:45:22
 * javazgw@gmail.com
 */
public class RatifyX {

	
	Vector<RatifyObject> v = new Vector<RatifyObject>();
	String busicode;//业务编码
	String busiicode;//单据内码
	String bcode;//单据所属部门人员
	public RatifyX()
	{
		
	}
	public RatifyX(Vector<RatifyObject>  v)
	{
		this.v = v;
	}
	
	
	
	/**
	 * 查看是否已经生成了审批记录，审批记录是在第一次审批的时候生成
	 * @return
	 * @throws Exception 
	 */
	public boolean havaRatifyDetail() throws Exception
	{
		String sql = "select cast(count(*) as int) as c from ratifydetail where busicode='"+busicode+"' and busiinnercode='"+busiicode+"'";
		PoolMan pm = new PoolMan();
		
		int count = 0;
		ResultSet rs = pm.executeQuery(sql);
		if(rs.next())
		{
			count = rs.getInt("c");
		}
		return count>0;
//		return false;
	}
	
	/**
	 * 生成审批记录，只有第一次提交审批的时候才生成审批记录
	 * @throws SQLException 
	 */
	public boolean makeRatifyDetail() throws SQLException
	{
		 PoolMan pm = new PoolMan();
		  CallableStatement proc = null;
		  proc =  pm.getConnection("test").prepareCall("{call Insert_New_Ratify(?,?,?,?)}");
		  proc.setString(1	, bcode);
		
		 // proc.setInt(2, 1);

		//  proc.registerOutParameter(2,java.sql.Types.INTEGER);  
		//  proc.setInt(2, 1);
		  proc.setString(2	, busiicode);
		  proc.setString(3	, busicode);
		  proc.registerOutParameter(4,java.sql.Types.INTEGER);  
		  proc.setInt(4, 0);
		  proc.executeUpdate();
		  int  res = proc.getInt(4);
		  return res>0;
	}
	
	/**
	 * 获得审批链的单元
	 * @return
	 */
	public Vector<RatifyObject> getRatifyVector()
	{
		return v;
	}
	/**
	 * 
	 * @param busicode 业务编码
	 * @param busiicode 单据内码
	 * @return
	 * @throws Exception 
	 */
	public Vector<RatifyObject> setRatifyVectorDB() throws Exception
	{
		String sql = "select * from ratifydetail where busicode='"+busicode+"' and busiinnercode='"+busiicode+"'";
		PoolMan pm = new PoolMan();
		Vector<RatifyObject> v =new Vector<RatifyObject>();
		ResultSet rs=null;
		try
		{
		 rs = pm.executeQuery(sql);
		 while(rs.next())
		 {
			 RatifyJDBC ro = new RatifyJDBC(busicode,busiicode);
			 ro.setIcode(rs.getString("code"));
			 ro.setTostatus(rs.getString("tostatus"));
			 ro.setIsfollow(rs.getInt("isfollow")==1?true:false);
			 String downratifycode = rs.getString("downratifycode");
			 if(downratifycode!=null)
			 {
				 for(String str :downratifycode.split(";"))
				 {
				 ro.addRatifyDownCodeStr(str);
				 }
			 }
//			 rs.getString("code");
//			 rs.getString("code");
//			 rs.getString("code");
//			 rs.getString("code");
//			 rs.getString("code");
			 v.add(ro);
		 }
		}
		finally
		{
			rs.close();
			pm.close("test");
		}
		
		for(RatifyObject ro:v)
		{
			for(String str:ro.getRatifyDownCodeStr())
			{
				for(RatifyObject ro2:v)
				{
					if(str.equals(ro2.getIcode()))
					{
						ro.addDownRatifyObject(ro2);
					}
				}
			}
			
		}
		//--------------------测试
		v.get(0).ratify();
		v.get(3).ratify();
		for(RatifyObject ro:v)
		{
			System.out.println(ro);
		}
		//--------------------测试
		return v;
	
	}
	
	/**单据所属部门人员
	 * @return the bcode
	 */
	public String getBcode() {
		return bcode;
	}
	/**
	 * @param bcode the bcode to set
	 */
	public void setBcode(String bcode) {
		this.bcode = bcode;
	}
	/**
	 * 业务编码
	 * @return the busicode
	 */
	public String getBusicode() {
		return busicode;
	}
	/**
	 * @param busicode the busicode to set
	 */
	public void setBusicode(String busicode) {
		this.busicode = busicode;
	}
	/**单据内码
	 * @return the busiicode
	 */
	public String getBusiicode() {
		return busiicode;
	}
	/**
	 * @param busiicode the busiicode to set
	 */
	public void setBusiicode(String busiicode) {
		this.busiicode = busiicode;
	}
	
	
	
}
